package LeetCode;

import java.util.Arrays;

/**
 * @Classname LC105
 * @Description [Tree]// https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/submissions/
 * @Date 2021/4/19 10:36
 * @Created by yin
 */

// Arrays.copyOfRange()
public class LC105 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length==0) return null;
        TreeNode root = new TreeNode(preorder[0]);
        for(int i=0;i<inorder.length;i++){
            if(preorder[0]==inorder[i]){
                root.left = buildTree(Arrays.copyOfRange(preorder,1,i+1),Arrays.copyOfRange(inorder,0,i));
                root.right = buildTree(Arrays.copyOfRange(preorder,i+1,inorder.length),Arrays.copyOfRange(inorder,i+1,inorder.length));
            }
        }
        return root;
    }
}
